package com.adee.algorithm.graph;

public class Test004_graph_DFS {
    public static void main(String[] args) {
        Graph4 g = new Graph4();
        g.build();
        g.DFS(0);
    }

    static class Graph4 {
        // 顶点数量
        public int v_count = 5;
        // 邻接矩阵
        public int[][] m = new int[v_count][v_count];
        // 标记顶点v是否被访问过，初始值为false
        public boolean[] visited = new boolean[v_count];

        public void build() {
            m[0][1] = 1;
            m[0][2] = 1;
            m[0][3] = 1;
            m[1][3] = 1;
            m[2][3] = 1;
            m[3][4] = 1;

            m[1][0] = 1;
            m[2][0] = 1;
            m[3][0] = 1;
            m[3][1] = 1;
            m[3][2] = 1;
            m[4][3] = 1;
        }

        public void DFS(int v) {
            visit(v);
            visited[v] = true;
            for (int i = 0; i < v_count; i++) {
                if (m[v][i] == 1 && !visited[i]) {
                    DFS(i);
                }
            }
        }

        public void visit(int v) {
            System.out.println("[" + v + "]");
        }
    }

}
